package com.bluefox.modules.monitor.rest;

import com.bluefox.modules.monitor.domain.Server;
import com.bluefox.modules.monitor.service.ServerService;
import com.bluefox.modules.monitor.service.dto.ServerQueryCriteria;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Set;

/**
 * @author Zhang houying
 * @date 2019-11-03
 */
@RestController
@RequestMapping("/api/server")
public class ServerController {

    private final ServerService serverService;

    public ServerController(ServerService serverService) {
        this.serverService = serverService;
    }

    @GetMapping

    @PreAuthorize("@el.check('server:list')")
    public ResponseEntity<Object> getServers(ServerQueryCriteria criteria, Pageable pageable) {
        return new ResponseEntity<>(serverService.queryAll(criteria, pageable), HttpStatus.OK);
    }

    @PostMapping

    @PreAuthorize("@el.check('server:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody Server resources) {
        return new ResponseEntity<>(serverService.create(resources), HttpStatus.CREATED);
    }

    @PutMapping

    @PreAuthorize("@el.check('server:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody Server resources) {
        serverService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @PreAuthorize("@el.check('server:del')")
    public ResponseEntity<Object> delete(@RequestBody Set<Long> ids) {
        serverService.delete(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}
